package com.haois.mp.wechat.server;

import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;

import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.offaccounts.service.RemindVehicleService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import com.haois.mp.database.module.common.daomain.Driver;
import com.haois.mp.database.module.common.daomain.Vehicle;
import com.haois.mp.database.module.offaccounts.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/9/5 0:38
 */
@Component("DriverServer")
public class DriverServerImpl extends AbstractMoreInfoServer {
    @Autowired
    private RemindVehicleService remindVehicleService;
    @Value("${defined.wxmp.sys-openid}")
    private String sysOpenId;
    @Autowired
    private VehicleService vehicleService;

    @Override
    public String executePlus(WxMpXmlMessage wxMpXmlMessage, Object object) {
        UserToken userToken = (UserToken) object;
        int pushSize = 0;
        String result = "";
        Reply reply = null;
        if (userToken != null) {
            String texts[] = wxMpXmlMessage.getContent().split(" ");
            if (texts[0].equals("信息更新")) {
                if (texts.length == 2) {
                    if (texts[1].equals("行驶证")) {
                        pushSize = remindVehicleService.vehicleValidPeriod(userToken.getUserId());
                    } else if (texts[1].equals("驾驶证")) {
                        pushSize = remindVehicleService.driverValidPeriod(userToken.getUserId());
                    } else if (texts[1].equals("违章信息推送")) {
                        if (!sysOpenId.equals(userToken.getOpenId())) {
                            return "提醒：该功能仅限管理员使用";
                        } else {
                            pushSize = remindVehicleService.vehiclePeccancy(true);
                            return "提醒：违章信息更新成功，共推送" + pushSize + "条";
                        }
                    }
                    if (pushSize == 0) {
                        return "提醒：不存在关联的对应信息，请先联系管理员添加";
                    }
                }
            } else if (texts[0].equals("增加行驶证")) {
                if (texts.length == 6) {
                    //新增行驶证请发送 【车牌号码 城市 车辆识别代号 发动机号 注册时间】
                    Vehicle vehicle = new Vehicle();
                    vehicle.setPlate(texts[1]);
                    vehicle.setCity(texts[2]);
                    vehicle.setVin(texts[3]);
                    vehicle.setEngineNo(texts[4]);
                    vehicle.setRegister(texts[5]);
                    reply = vehicleService.insertVehicle(vehicle, userToken.getUserId());
                } else {
                    return "格式有误,请重新发送";
                }

            } else if (texts[0].equals("增加驾驶证")) {
                if (texts.length == 5) {
                    //新增驾驶证请发送 【姓名 档案号 证件号码 注册时间】
                    Driver driver = new Driver();
                    driver.setName(texts[1]);
                    driver.setDossier(texts[2]);
                    driver.setLicenseNo(texts[3]);
                    driver.setRegister(texts[4]);
                    reply = vehicleService.insertDriver(driver, userToken.getUserId());
                } else {
                    return "格式有误,请重新发送";
                }
            }
        }
        if (!reply.getCode().equals(ApiResultEnum.API_00.getCode())) {
            String[] re = result.split(":");
            return re[1];
        } else {
            return "success";
        }
    }
}
